package com.yanqu.road.server.cmd.activity.zhenhuan;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ZhenHuanProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.zhenhuan.CrossZhenHuanMgr;
import com.yanqu.road.server.manager.activity.zhenhuan.CrossZhenHuanThirdProcessActivity;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = CrossProtocol.C_CROSS_ZHENHUAN_THIRD_PROCESS_OPEN_BANQUET, desc = "开宴会")
public class CrossZhenHuanThirdProcessOpenBanquetCmd extends CrossCommand {

    private int activityId;

    private PlayerProto.PlayerBaseTempMsg playerBaseData;

    private long userId;

    private int openItemId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhenHuanProto.CrossZhenHuanThirdProcessOpenBanquetReqMsg reqMsg = ZhenHuanProto.CrossZhenHuanThirdProcessOpenBanquetReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        playerBaseData = reqMsg.getPlayerBaseData();
        userId = packet.getPlayerId();
        openItemId = reqMsg.getOpenItemId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossZhenHuanThirdProcessActivity activity = CrossZhenHuanMgr.getThirdProcessActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_ACTIVITY_NOT_FOUND;
            return;
        }
        // 更新一下用户数据
        UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(playerBaseData);
        CrossUserMgr.updateUserBaseInfo(userId, userBaseInfo);

        // 开宴会
        ret = activity.openBanquetByCmd(serverId, userId, openItemId);
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            ZhenHuanProto.CrossZhenHuanThirdProcessOpenBanquetRespMsg.Builder respMsg = ZhenHuanProto.CrossZhenHuanThirdProcessOpenBanquetRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setOpenItemId(openItemId);
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(Protocol.S_ZHENHUAN_THIRD_PROCESS_OPEN_BANQUET_FROM_CROSS, respMsg));
        }
    }
}
